package com.bootdo.program.wallpaper.controller;

import java.net.MalformedURLException;
import java.util.*;

import com.bootdo.common.controller.BaseController;
import com.bootdo.program.wallpaper.domain.BzAllWallpaperDO;
import com.bootdo.program.wallpaper.domain.BzClassificationDO;
import com.bootdo.program.wallpaper.domain.BzWallpaperDO;
import com.bootdo.program.wallpaper.service.BzAllWallpaperService;
import com.bootdo.program.wallpaper.service.BzClassificationService;
import com.bootdo.program.wallpaper.service.BzWallpaperService;
import com.bootdo.reptile.WxImageUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 小程序壁纸表
 * 
 * @author guoyunfei
 * @email 2549789059@qq.com
 * @date 2021-12-05 03:18:50
 */
 
@Controller
@RequestMapping("/wallpaper/bzWallpaper")
public class BzWallpaperController extends BaseController {

	@Value("${web.forward.url}")
	private String forwardUrl;

	@Autowired
	private BzWallpaperService bzWallpaperService;

	@Autowired
	private BzClassificationService bzClassificationService;

	@Autowired
	private BzAllWallpaperService bzAllWallpaperService;
	
	@GetMapping()
	@RequiresPermissions("wallpaper:bzWallpaper:bzWallpaper")
	String BzWallpaper(){
	    return "wallpaper/bzWallpaper/bzWallpaper";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wallpaper:bzWallpaper:bzWallpaper")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<BzWallpaperDO> bzWallpaperList = bzWallpaperService.list(query);

		for (int i = 0; i < bzWallpaperList.size(); i++) {
			BzClassificationDO classificationDO = bzClassificationService.get(bzWallpaperList.get(i).getClassId());

			if(null != classificationDO){
				bzWallpaperList.get(i).setClassName(classificationDO.getName());
			}
		}

		int total = bzWallpaperService.count(query);
		PageUtils pageUtils = new PageUtils(bzWallpaperList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wallpaper:bzWallpaper:add")
	String add(Model model){

		Map<String, Object> map = new HashMap<>();
		map.put("enable",1);
		List<BzClassificationDO> list = bzClassificationService.list(map);

		model.addAttribute("list",list);

		return "wallpaper/bzWallpaper/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wallpaper:bzWallpaper:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		BzWallpaperDO bzWallpaper = bzWallpaperService.get(id);
		model.addAttribute("bzWallpaper", bzWallpaper);

		Map<String, Object> map = new HashMap<>();
		map.put("enable",1);
		List<BzClassificationDO> list = bzClassificationService.list(map);
		model.addAttribute("list",list);
		return "wallpaper/bzWallpaper/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wallpaper:bzWallpaper:add")
	public R save( BzWallpaperDO bzWallpaper){
		if(bzWallpaperService.save(bzWallpaper)>0){
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/batchSave")
	@RequiresPermissions("wallpaper:bzWallpaper:add")
	public R batchSave( BzWallpaperDO bzWallpaper) throws MalformedURLException {

		String wxUrl = "mp.weixin.qq.com";

		if(StringUtils.isEmpty(bzWallpaper.getAddress().trim())){
			return R.error("请输入需要解析的公众号地址");
		}

		String[] split = bzWallpaper.getAddress().trim().split(",");

		for (int i = 0; i < split.length; i++) {
			if(split[i].indexOf(wxUrl) < 0){
				return R.error("输入的地址不正确，请输入需要解析的公众号地址");
			}
		}

		List<BzWallpaperDO> list = new ArrayList<>();

		List<String> strings = WxImageUtil.imgUrls(bzWallpaper.getAddress());

		for (int i = 0; i < strings.size(); i++) {

			if(StringUtils.isEmpty(strings.get(i))){
				continue;
			}
			BzWallpaperDO wallpaper = new BzWallpaperDO();
			wallpaper.setClassId(bzWallpaper.getClassId());


			//int index = strings.get(i).indexOf("?");

			//String s = strings.get(i).substring(0, index);

			//wallpaper.setImage(s);

			wallpaper.setImage(strings.get(i));

			wallpaper.setTime(new Date());
			wallpaper.setIsDelete(1);
			wallpaper.setTags("公众号");
			wallpaper.setStatus(0);
			java.net.URL  url = new  java.net.URL(strings.get(i));
			String host = url.getHost();// 获取主机名
			wallpaper.setTitle(host);

			list.add(wallpaper);
		}

		if(bzWallpaperService.batchSave(list)>0){
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wallpaper:bzWallpaper:edit")
	public R update( BzWallpaperDO bzWallpaper){
		bzWallpaperService.update(bzWallpaper);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wallpaper:bzWallpaper:remove")
	public R remove( Integer id){
		if(bzWallpaperService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wallpaper:bzWallpaper:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids){
		bzWallpaperService.batchRemove(ids);
		return R.ok();
	}

	@PostMapping( "/batchSyncData")
	@ResponseBody
	@RequiresPermissions("wallpaper:bzWallpaper:batchSyncData")
	public R batchSyncData(@RequestParam("ids[]") Integer[] ids){


		for (int i = 0; i < ids.length; i++) {
			BzWallpaperDO wallpaperDO = bzWallpaperService.get(ids[i]);

			Date date = new Date();
			BzAllWallpaperDO bzAllWallpaperDO = new BzAllWallpaperDO();
			bzAllWallpaperDO.setImage(forwardUrl + wallpaperDO.getImage());
			bzAllWallpaperDO.setClassId(wallpaperDO.getClassId());
			bzAllWallpaperDO.setThumbnail(wallpaperDO.getImage());
			bzAllWallpaperDO.setTime(date);
			bzAllWallpaperDO.setStatus(2);
			bzAllWallpaperDO.setIsDelete(1);
			bzAllWallpaperDO.setCreateTime(date);
			bzAllWallpaperDO.setUpdateTime(date);
			//bzAllWallpaperDO.setAppid(getUser().getAppid());
			bzAllWallpaperDO.setIsSys(1);
			bzAllWallpaperDO.setLowImageQuality(forwardUrl + wallpaperDO.getImage());
			bzAllWallpaperService.save(bzAllWallpaperDO);

			bzWallpaperService.remove(ids[i]);
		}

		return R.ok();
	}

	@PostMapping( "/syncData")
	@ResponseBody
	public R syncData(Integer id){

		BzWallpaperDO wallpaperDO = bzWallpaperService.get(id);

		Date date = new Date();
		BzAllWallpaperDO bzAllWallpaperDO = new BzAllWallpaperDO();
		bzAllWallpaperDO.setImage(forwardUrl + wallpaperDO.getImage());
		bzAllWallpaperDO.setClassId(wallpaperDO.getClassId());
		bzAllWallpaperDO.setThumbnail(wallpaperDO.getImage());
		bzAllWallpaperDO.setTime(date);
		bzAllWallpaperDO.setStatus(2);
		bzAllWallpaperDO.setIsDelete(1);
		bzAllWallpaperDO.setCreateTime(date);
		bzAllWallpaperDO.setUpdateTime(date);
		//bzAllWallpaperDO.setAppid(getUser().getAppid());
		bzAllWallpaperDO.setIsSys(1);
		bzAllWallpaperDO.setLowImageQuality(forwardUrl + wallpaperDO.getImage());
		bzAllWallpaperService.save(bzAllWallpaperDO);

		bzWallpaperService.remove(id);
		return R.ok();
	}
	
}
